package springboot.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/*
* 延迟交换机
* 与队列的绑定
*
* */
@Configuration
public class DelayRabbitMQConfig {
    public static final String DELAY_QUEUE="delay_queue";
    public static final String DELAY_EXCHANGE="delay_exchange";
    public static final String DELAY_ROUTING_KEY="delay_routing_key";
    /*队列的声明*/
    @Bean
    public Queue delayQueue(){
        return QueueBuilder.durable(DELAY_QUEUE).build();
    }
    /*自定义交换机的声明*/
    /*基于插件的交换机声明*/
    @Bean
    public CustomExchange delayExchange(){
        Map<String, Object> arguments=new HashMap<>();
        /*延迟类型*/
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(DELAY_EXCHANGE,"x-delayed-message",true,false,arguments);
    }
    /*交换机与队列的绑定*/
    @Bean
    public Binding delayQueueBindingDelayExchange(@Qualifier("delayQueue") Queue delayQueue,
                                                  @Qualifier("delayExchange") CustomExchange delayExchange){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with(DELAY_ROUTING_KEY).noargs();
    }
}
